#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : DMainWindow(parent)
{
//    this->setFixedSize(800,600);

    // 启动
    startProcess = new QProcess;
    startProcess = new QProcess(this);

    isSucessed = isConnectSucess();

    if(isSucessed==true){

        this->setCentralWidget(new SucessConnectWidget(this));
    }else{
        this->setCentralWidget(new FailContentWidget(this));
    }


}


void MainWindow::runBash()
{
    startProcess->start("ls", QStringList() << "-al" << "/");
    startProcess->waitForFinished();

}

bool MainWindow::isConnectSucess(){
    // 判断是否连接成功
    startProcess -> start("adb", QStringList() << "devices");
    bool finishStatus = startProcess->waitForFinished();
    if(finishStatus == true){
        QString output = QString::fromLocal8Bit(startProcess->readAllStandardOutput());
        QStringList osl = output.split("\n");

        //"List of devices attached"
        //"524c2ea9\tdevice"

        if(osl.length() >= 2){
            // 如果osl的列表长>2 表示有设备接入
            QString deviceString = osl[1];
            deviceString = deviceString.trimmed();   // 去掉两边空白
            if(deviceString != ""){
                QStringList ds = deviceString.split("\t");
                QMessageBox::information(0, "连接成功", "设备："+ ds[0] + "连接成功");      // 获取设备号
                return true;
            }
            return false;
        }

        return true;
    }

    return false;
}
void MainWindow::on_readoutput()
{
    QString output = QString::fromLocal8Bit(startProcess->readAllStandardOutput());
    QStringList sl = output.split("\n");
    foreach (QString s, sl) {
        qDebug() << s;
    }

}

void MainWindow::on_readError()
{

}


